<?php

/**
 * 举报
 */
namespace app\admin\controller;

use app\admin\service\ReportClassifyService;
use cmf\controller\AdminBaseController;
use think\facade\Db;

class ReportController extends AdminbaseController {
    protected function getStatus($k=''){
        $status=[
            '0'=>'待处理',
            '1'=>'已处理',
        ];
        
        if($k==''){
            return $status;
        }
        return $status[$k];
    }
    function index(){
        $data = $this->request->param();
        $map=[];
        
        $start_time=isset($data['start_time']) ? $data['start_time']: '';
        $end_time=isset($data['end_time']) ? $data['end_time']: '';
        
        if($start_time!=""){
           $map[]=['addtime','>=',strtotime($start_time)];
        }

        if($end_time!=""){
           $map[]=['addtime','<=',strtotime($end_time) + 60*60*24];
        }
        
        $status=isset($data['status']) ? $data['status']: '';
        if($status!=''){
            $map[]=['status','=',$status];
        }
        
        $uid=isset($data['uid']) ? $data['uid']: '';
        if($uid!=''){
            $map[]=['uid','=',$uid];
        }	
			

    	$lists = Db::name("user_report")
                ->where($map)
                ->order("id DESC")
                ->paginate(20);
        
        $lists->each(function($v,$k){
			$v['userinfo']=getUserInfo($v['uid']);
            $user_status=Db::name("user")->where("id={$v['touid']}")->value('user_status');
            $touserinfo=getUserInfo($v['touid']);
            $touserinfo['user_status']=$user_status;
			$v['touserinfo']=$touserinfo;
            return $v;           
        });
		
	
        
        $lists->appends($data);
        $page = $lists->render();

    	$this->assign('lists', $lists);

    	$this->assign("page", $page);
        
    	$this->assign("status", $this->getStatus());
    	
    	return $this->fetch();
    }	
    function setstatus(){
        $id = $this->request->param('id', 0, 'intval');
        $data['status']=1;
        $data['uptime']=time();
        $data=[
            'status'=>1,
            'uptime'=>time(),
        ];
        $rs = Db::name('user_report')->where("id={$id}")->update($data);
        if($rs===false){
            $this->error("标记失败！");
        }

        $this->success("标记成功！");
        							  		
    }
    function del(){
        
        $id = $this->request->param('id', 0, 'intval');
        
        $rs = Db::name('user_report')->where("id={$id}")->delete();
        if(!$rs){
            $this->error("删除失败！");
        }
        $this->success("删除成功！",url("report/index"));								  
    }
	
	
	//举报分类
	function classify(){
        $data = $this->request->param();
        if (empty($data['langId'])) {
            $data['langId'] = $this->defaultLanguage['id'];
            $this->assign('langId', $this->defaultLanguage['id']);
        }else {
            $this->assign('langId', $data['langId']);
        }

        $data['type'] = 1;
        $lists = ReportClassifyService::getData($data);
//        $map=[];
//        $keyword=isset($data['keyword']) ? $data['keyword']: '';
//        if($keyword!=''){
//            $map[]=['name','like',"%".$keyword."%"];
//        }
//        $lists = Db::name("user_report_classify")
//                ->where($map)
//                ->order("list_order asc")
//                ->paginate(20);
        $lists->appends($data);
        $page = $lists->render();
    	$this->assign('lists', $lists);
    	$this->assign("page", $page);
        $this->assign('countryData', $this->recommendLanguageData);
    	return $this->fetch();
    }
	//分类添加	
    public function classify_add(){
        $this->assign('countryData', $this->recommendLanguageData);
        return $this->fetch();
    }
	public function classify_addPost(){
		if ($this->request->isPost()) {       
            $data      = $this->request->param();
            $data['type'] = 1;
            $data['name']=trim($data['name']);
			$name=$data['name'];
			if($name==""){
				$this->error("名称不能为空");
			}
            $condition = [
                'del_flag' => 0,
                'name'=>$name,
                'lang_id'=>$data['lang_id'],
                'type' => 1,
            ];
            $isexit=Db::name('user_report_classify')->where($condition)->find();
            if($isexit){
                $this->error('该名称已存在');
            }
            
            $data['addtime']=time();
			$id = Db::name('user_report_classify')->insertGetId($data);
            if(!$id){
                $this->error("添加失败！");
            }
			$this->resetcache();
            $this->success("添加成功！");
            
		}			
	}
	
	
	
	
	//分类编辑
    public function classify_edit(){
        $id   = $this->request->param('id', 0, 'intval');

        $data=Db::name('user_report_classify')
            ->where("id={$id}")
            ->find();
        if(!$data){
            $this->error("信息错误");
        }
        
        $this->assign('data', $data);
        return $this->fetch();			
    }
	public function classify_editPost(){
		if ($this->request->isPost()) {
            $data      = $this->request->param();
            $classData = Db::name('user_report_classify')->where(['id' => $data['id']])->find();
            $data['name']=trim($data['name']);
			$name=$data['name'];
			$id=$data['id'];

			if($name==""){
				$this->error("名称不能为空");
			}

            $condition = [
                'del_flag' => 0,
                'name'=>$name,
                'type' => 1,
                'lang_id' => $classData['lang_id']
            ];
            $isexit = Db::name('user_report_classify')->where($condition)->where([['id','<>',$id]])->find();
            if($isexit){
                $this->error('该名称已存在');
            }
			$rs = Db::name('user_report_classify')->update($data);
            if($rs===false){
                $this->error("修改失败！");
            }
            
			
			$this->resetcache();
            $this->success("修改成功！");
		}	
	}
	
	//分类删除
	public function classify_del(){
        $id = $this->request->param('id', 0, 'intval');
        
//        $rs = Db::name('user_report_classify')->where("id={$id}")->delete();
        $rs = DB::name('user_report_classify')->where("id={$id}")->update(['del_flag' => 1]);
        if(!$rs){
            $this->error("删除失败！");
        }
		
		
		$this->resetcache();
        $this->success("删除成功！",url("report/classify"));
            
	}
	
	
	 //分类排序
    public function classify_listOrder() { 
		
        $model = Db::name('user_report_classify');
        parent::listOrders($model);
        
		
		$this->resetcache();
        $this->success("排序更新成功！");
        
    }
	
	//分类缓存
	protected function resetcache(){
        $key='getUserReportClassify';
        $classify = DB::name('user_report_classify')
            ->where('type',1)
            ->field('id,name')
            ->order("list_order asc")
            ->select();

        if($classify){
            setcaches($key,$classify);
        }
    }
	
	
    
	

    
}
